/* 
 * Copyright (c) Sergey B. Berezin (s_berezin@cs.msu.su), 2002, 2003.
 *
 * You may freely distribute or modify this source code retaining the 
 * above copyright notice.
 *
 * This software is provided "as-is" and without guarantee or warrantee 
 * of any kind, expressed or implied :)
 */

using System;
using System.Drawing;
using System.Windows.Forms;

using OpenGL;
using ManagedGL;

namespace UVS.UI {

	public class Layer2d : Layer {
		
		public override void OnRender() {
			IViewport v = view.Viewport;
			if(v == null)
				return;   // No viewport made current
			Viewport2d viewport = v as Viewport2d;
			if(viewport == null)
				return;   // Current viewport is not 2d viewport			
			OnRender(viewport);
		}

		public virtual void OnRender(Viewport2d viewport) {
			// Nothing to do here
		}
	}

	public class Viewport2dLayer : SelectorLayer, IViewportLayer {

		protected Viewport2d viewport = new Viewport2d();

		public Viewport2d Viewport2d {
			get {
				return viewport;
			}
		}

		public IViewport Viewport {
			get {
				return viewport;
			}
		}

		public override void OnRender() {
			base.OnRender();
			GL.glMatrixMode(GL.GL_PROJECTION);
			GL.glLoadIdentity();
			GL.glOrtho(0,Width,0,Height,-1,1);
			GL.glMatrixMode(GL.GL_MODELVIEW);
			GL.glLoadIdentity();
		}
			
		public override void OnResize() {
			base.OnResize();
			viewport.Window = new Rectangle(0,0,Width,Height);
		}

		public override void OnSelect(Rectangle selection) {
			base.OnSelect(selection);
			selection.Y = Height - selection.Y - selection.Height;
			viewport.ZoomIn(selection);
			view.UpdateAllLayers();
		}

		public void OnZoomOutToOriginal(Object sender,EventArgs a) {
			viewport.ZoomOutToOriginal();
			view.UpdateAllLayers();
		}

		public void OnZoomOutOneLevel(Object sender,EventArgs a) {
			//Viewport.ZoomOutOneLevel();
			view.UpdateAllLayers();
		}


		public override void UpdateContextMenu(ContextMenu menu) {
			base.UpdateContextMenu(menu);
			menu.MenuItems.Add(new MenuItem("&Zoom out one level",new EventHandler(this.OnZoomOutOneLevel)));
			menu.MenuItems.Add(new MenuItem("Zoom out to &original",new EventHandler(this.OnZoomOutToOriginal)));
		}
	}
}